package chances.epg.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import chances.epg.api.controller.model.ChannelCategoryItem;
import chances.epg.api.controller.model.ResultSetResponse;
import chances.epg.commons.utils.RequestUtils;
import chances.epg.data.PageBean;
import chances.epg.entity.content.CategoryItem;
import chances.epg.entity.content.Channel;

@RequestMapping("/channel")
@RestController
public class ChannelDataController extends AbstractDataController {

	@RequestMapping("/category/{categoryCode}")
	public ResultSetResponse<ChannelCategoryItem> getChannelByCategory(@PathVariable("categoryCode") String categoryCode, HttpServletRequest request) {
		int size = RequestUtils.getParameter(request, "size", DEFAULT_SIZE);
		int pageIndex = RequestUtils.getParameter(request, "pageindex", 1);
		PageBean pageBean = super.createPageBean(size, pageIndex);
		List<CategoryItem> items = this.categoryService.listCategoryItem(categoryCode, "channel", 0, pageBean);
		List<ChannelCategoryItem> channelInfos = new ArrayList<ChannelCategoryItem>();
		this.setResultUrl(items, request);
		for (CategoryItem categoryItem : items) {

			String code = categoryItem.getItemCode();
			Channel channel = this.channelService.getChannelByCode(code);
			categoryItem.setSource(channel);
			ChannelCategoryItem channelInfo = new ChannelCategoryItem(categoryItem);
			channelInfo.setCurrent(this.channelService.getNowShcedule(code));
			channelInfo.setNext(this.channelService.getNextShcedule(code));
			// channelInfo.setPrev(this.channelService.getPrevSchedule(code));
			channelInfos.add(channelInfo);
		
		}
		return new ResultSetResponse<ChannelCategoryItem>(SUCCESS,channelInfos,pageBean);
	}
}
